﻿using MiniFox.Components;
using Wechat.Corp.Message.Configurations;
using Wechat.Corp.Message.Models;

namespace Wechat.Corp.Message.Services
{
    /// <summary>
    /// 
    /// </summary>
    public class WechatAgentMessageService : WechatAgentService, IWechatAgentMessageService
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        public WechatAgentMessageService(string name) : base(name)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="setting"></param>
        public WechatAgentMessageService(IWechatAgent setting) : base(setting)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        [AutoConfigure]
        protected WechatCorpMessageConfiguration MessageConfiguration { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public Uri MessageSendUri
        {
            get
            {
                return new Uri(MessageConfiguration.MessageSendUri);
            }
        }
        #region message

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="userIds"></param>
        /// <returns></returns>
        protected virtual T CreateMessageRequest<T>(params string[] userIds) where T : MessageRequest, new()
        {
            ArgumentNullException.ThrowIfNull(userIds);

            T msg = new()
            {
                AgentID = this.AgentID,
                UserList = userIds
            };
            return msg;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendMessageAsync(MessageRequest request) //where MRQ : MessageRequest where MRS : MessageResponse
        {
            return this.HttpPostAsync<MessageRequest, MessageResponse>(this.MessageSendUri, request);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendTextMessage(string content, params string[] userIds)
        {
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentException($"“{nameof(content)}”不能为 null 或空。", nameof(content));
            }
            var request = this.CreateMessageRequest<TextMessageRequest>(userIds);
            request.Content = content;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendImageMessage(string mediaId, params string[] userIds)
        {
            if (string.IsNullOrEmpty(mediaId))
            {
                throw new ArgumentException($"“{nameof(mediaId)}”不能为 null 或空。", nameof(mediaId));
            }

            var request = this.CreateMessageRequest<ImageMessageRequest>(userIds);
            request.MediaId = mediaId;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendVoiceMessage(string mediaId, params string[] userIds)
        {
            if (string.IsNullOrEmpty(mediaId))
            {
                throw new ArgumentException($"“{nameof(mediaId)}”不能为 null 或空。", nameof(mediaId));
            }

            var request = this.CreateMessageRequest<VoiceMessageRequest>(userIds);
            request.MediaId = mediaId;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendVideoMessage(string mediaId, params string[] userIds)
        {
            if (string.IsNullOrEmpty(mediaId))
            {
                throw new ArgumentException($"“{nameof(mediaId)}”不能为 null 或空。", nameof(mediaId));
            }

            var request = this.CreateMessageRequest<VideoMessageRequest>(userIds);
            request.MediaId = mediaId;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="mediaId"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendFileMessage(string mediaId, params string[] userIds)
        {
            if (string.IsNullOrEmpty(mediaId))
            {
                throw new ArgumentException($"“{nameof(mediaId)}”不能为 null 或空。", nameof(mediaId));
            }

            var request = this.CreateMessageRequest<FileMessageRequest>(userIds);
            request.MediaId = mediaId;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        /// <param name="url"></param>
        /// <param name="btnText"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public Task<MessageResponse> SendTextCardMessage(string title, string text, string url, string btnText, params string[] userIds)
        {
            if (string.IsNullOrEmpty(title))
            {
                throw new ArgumentException($"“{nameof(title)}”不能为 null 或空。", nameof(title));
            }
            if (string.IsNullOrEmpty(text))
            {
                throw new ArgumentException($"“{nameof(text)}”不能为 null 或空。", nameof(text));
            }
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url));
            }


            var request = this.CreateMessageRequest<TextCardMessageRequest>(userIds);
            request.Title = title;
            request.Url = url;
            request.Description = text;
            request.BtnText = btnText;
            return this.SendMessageAsync(request);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="title"></param>
        /// <param name="text"></param>
        /// <param name="url"></param>
        /// <param name="pictureUrl"></param>
        /// <param name="appId"></param>
        /// <param name="pagePath"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        /// <exception cref="ArgumentException"></exception>
        public Task<MessageResponse> SendNewsMessage(string title, string text, string url, string pictureUrl, string appId, string pagePath, params string[] userIds)
        {
            if (string.IsNullOrEmpty(title))
            {
                throw new ArgumentException($"“{nameof(title)}”不能为 null 或空。", nameof(title));
            }
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentException($"“{nameof(url)}”不能为 null 或空。", nameof(url));
            }

            var request = this.CreateMessageRequest<NewsMessageRequest>(userIds);
            request.Articles.Add(new NewsArticle
            {
                Title = title,
                Url = url,
                Description = text,
                PictureUrl = pictureUrl,
                AppId = appId,
                PagePath = pagePath
            });
            return this.SendMessageAsync(request);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="content"></param>
        /// <param name="userIds"></param>
        /// <returns></returns>
        public virtual Task<MessageResponse> SendMarkdownMessage(string content, params string[] userIds)
        {
            if (string.IsNullOrEmpty(content))
            {
                throw new ArgumentException($"“{nameof(content)}”不能为 null 或空。", nameof(content));
            }

            var request = this.CreateMessageRequest<MarkdownMessageRequest>(userIds);
            request.Content = content;
            return this.SendMessageAsync(request);
        }
        #endregion

    }
}
